﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
using BinaryComponents.SuperList;
using BinaryComponents.SuperList.Sections;
using BinaryComponents.Utility.Collections;
using ListControl = BinaryComponents.SuperList.ListControl;
using DataStorage;


namespace Booky.WinFormsClient
{
    public class EmailPreviewExample : IDisposable
    {
        public enum Style
        {
            AllRows,
            SelectedOnly
        } ;

        public EmailPreviewExample(ListControl listControl, Style style)
        {
            _oldFactory = listControl.SectionFactory; // store old factory as we want to leave as we came.
            _listControl = listControl;

            //
            // Replace the current SectionFactory with our override.
            listControl.SectionFactory = new MySectionFactory(style); // 
            _listControl.LayoutSections();
        }


        public void Dispose()
        {
            if (_oldFactory != null) // put things back as they were
            {
                _listControl.SectionFactory = _oldFactory;
                _listControl.LayoutSections();
            }
        }

        private class MySectionFactory : SectionFactory
        {
            public MySectionFactory(Style style)
            {
                _style = style;
            }

            public override RowSection CreateRowSection(
                    ListControl listControl,
                    RowIdentifier rowIdentifier,
                    HeaderSection headerSection,
                    int position)
            {
                return new MyRowSection(listControl, rowIdentifier, headerSection, position, _style);
            }

            private Style _style;
        }

        private class MyRowSection : RowSection
        {
            public MyRowSection(
                    ListControl listControl,
                    RowIdentifier rowIdentifier,
                    HeaderSection headerSection,
                    int position,
                    Style style)
                : base(listControl, rowIdentifier, headerSection, position)
            {
                _style = style;
            }

            private Font AutoPreviewFont
            {
                get
                {
                    return new Font(Host.Font, FontStyle.Italic);
                }
            }

            public override bool NeedsLayoutOnSelection
            {
                get
                {
                    return _style == Style.SelectedOnly;
                }
            }

            public override void Layout(GraphicsSettings gs, Size maximumSize)
            {
                base.Layout(gs, maximumSize);

                if (_style == Style.AllRows || IsSelected)
                {
                    using (Font autoPreviewFont = AutoPreviewFont)
                    {
                        int top = Size.Height;
                        int autoHeight = autoPreviewFont.Height * 2;
                        int indent = IndentWidth + 5;
                        Bookmark person = (Bookmark)Item;
                        string description = person.Description;

                        if (!string.IsNullOrEmpty(description))
                        {
                            _rect = new Rectangle(indent, top, Size.Width - indent - 20, autoHeight);

                            Size = new Size(Size.Width, Size.Height + autoHeight + 4);
                        }
                    }
                }
            }


            public override void Paint(GraphicsSettings gs, Rectangle clipRect)
            {
                base.Paint(gs, clipRect);

                if (_style == Style.AllRows || IsSelected)
                {
                    using (Font autoPreviewFont = AutoPreviewFont)
                    {
                        Bookmark person = (Bookmark)Item;
                        string description = person.Description;

                        Rectangle rect = _rect;

                        rect.Offset(HostBasedRectangle.Location);

                        Color color;

                        if (IsSelected)
                        {
                            color = SystemColors.HighlightText;
                        }
                        else
                        {
                            color = SystemColors.ControlDarkDark;
                        }

                        BinaryComponents.WinFormsUtility.Drawing.GdiPlusEx.DrawString
                                (gs.Graphics, description, AutoPreviewFont, color, rect
                                 , BinaryComponents.WinFormsUtility.Drawing.GdiPlusEx.TextSplitting.MultiLine, BinaryComponents.WinFormsUtility.Drawing.GdiPlusEx.Ampersands.Display);
                    }
                }
            }

            private Rectangle _rect;
            private Style _style;
        }

        private ListControl _listControl;
        private SectionFactory _oldFactory;
    }
}
